/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.zbxsoft.platform.filter  
 */
package com.dwusoft.platform.filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.cache.util.Validator;
import com.apache.license.filter.AbstractFilter;
import com.apache.task.api.ScheduledExecutor;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.PBOSSOTools;
import com.apache.uct.common.ToolsUtil;

/**
 * description:  系统license验证类
 * @author Hou Dayu 创建时间：2014-12-23  
 * 
 */
public class LicenseFilter extends AbstractFilter {

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see javax.servlet.Filter#destroy()  
	 */
	public void destroy() {

	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)  
	 */
	@SuppressWarnings({ "deprecation" })
	public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain arg2) throws IOException,
			ServletException {
		HttpServletRequest request = (HttpServletRequest) srequest;
		HttpServletResponse response = (HttpServletResponse) sresponse;
		//以下验证当前用户是否是有效组织方
		String path = request.getServletPath();
		LoginUser loginUser = PBOSSOTools.getLoginUser(request);
		boolean mark = !"/error.action".equals(path) || path.indexOf("/admin/hall") == -1
				|| path.indexOf("/wbd/service") == -1;
		if (!Validator.isEmpty(loginUser) && mark) {
			String superUser = ToolsUtil.getInstance().getValueByKey("wbd_supper_user");
			if (loginUser.getUserEname().equalsIgnoreCase(superUser)) {
				arg2.doFilter(request, response);
				return;
			}
			List<String> names = BidCacheFactory.getInstance().getBidOrgEnames();
			if (!names.contains(loginUser.getOrgEname())) {
				request.setAttribute("errors", "对不起,您没有访问权限");
				request.getRequestDispatcher("/WEB-INF/views/error/error.jsp").forward(request, response);
				return;
			}
		}
		arg2.doFilter(request, response);
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)  
	 */
	public void init(FilterConfig arg0) throws ServletException {
		this.initLincense();
		ScheduledExecutor.newInstance().initStart();
	}

}
